/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.uc.domain.converter.PositionConverter;
import com.xy.biz.uc.domain.query.PositionQuery;
import com.xy.biz.uc.domain.req.GetPositionByUserIdRequest;
import com.xy.biz.uc.service.PositionService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.uc.domain.resp.GetPositionResponse;
import com.xy.biz.uc.domain.req.GetPositionRequest;
import com.xy.biz.uc.domain.req.ListPositionRequest;
import com.xy.biz.uc.domain.req.SavePositionRequest;
import com.xy.biz.uc.client.PositionClient;

/**
 *
 *
 * @author zgy
 * @date 2021-9-23
 */
@Api(tags = "岗位服务")
@Slf4j
@RestController
@RequestMapping("/api/uc/position")
public class PositionController implements PositionClient {

    @Autowired
    private PositionService positionService;

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> savePosition(@RequestBody SavePositionRequest request) {
        return positionService.savePosition(PositionConverter.toBO(request));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllPosition(@RequestBody Collection<SavePositionRequest> collection) {
        positionService.saveAllPosition(PositionConverter.toBO(collection));
        return Result.success();
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updatePosition(@RequestBody SavePositionRequest request) {
        positionService.updatePosition(PositionConverter.toBO(request));
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deletePosition(@RequestBody GetPositionRequest request) {
        positionService.deletePosition(request.getPositionId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetPositionResponse> getPosition(@RequestBody GetPositionRequest request) {
        return Result.success(PositionConverter.toResponse(positionService.getPosition(request.getPositionId())));
    }

    @Validatable
    @PostMapping(value = "/get_by_user_id")
    @Override
    public Result<GetPositionResponse> getPositionByUserId(@RequestBody GetPositionByUserIdRequest request) {
        return Result.success(PositionConverter.toResponse(positionService.getPositionByUserId(request.getUserId())));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetPositionResponse>> listPositionById(@RequestBody Collection<Long> collection) {
        return Result.success(PositionConverter.toResponse(positionService.listPositionById(collection)));
    }

    @PostMapping(value = "/list_by_user_ids")
    @Override
    public Result<List<GetPositionResponse>> listPositionByUserId(@RequestBody Collection<Long> collection) {
        return Result.success(PositionConverter.toResponse(positionService.listPositionByUserId(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetPositionResponse>> listPosition(@RequestBody ListPositionRequest request) {
        return Result.success(PositionConverter.toResponse(positionService.listPosition(PositionConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countPosition(@RequestBody ListPositionRequest request) {
        return Result.success(positionService.countPosition(PositionConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetPositionResponse>> pagePosition(@RequestBody ListPositionRequest request) {
        PositionQuery query = PositionConverter.toQuery(request);
        List<GetPositionResponse> list = PositionConverter.toResponse(positionService.listPosition(query));
        int count = positionService.countPosition(query);
        return Result.success(new Page<>(list, count));
    }

}
